/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.activity;

import android.content.Intent;
import android.widget.EditText;

import com.ouertech.android.agm.lib.base.future.core.AgnettyFuture;
import com.ouertech.android.agm.lib.base.future.core.AgnettyResult;
import com.ouertech.android.agm.lib.base.utils.UtilLog;
import com.ouertech.android.agm.lib.base.utils.UtilMD5;
import com.ouertech.android.agm.lib.base.utils.UtilString;
import com.ouertech.android.agm.lib.ui.base.defaults.activity.BaseSystemActivity;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.PhoneAccessToken;
import com.simon.mengkou.data.bean.base.QQAccessToken;
import com.simon.mengkou.data.bean.base.SinaAccessToken;
import com.simon.mengkou.data.bean.base.WeixinAccessToken;
import com.simon.mengkou.data.bean.req.LoginReq;
import com.simon.mengkou.data.enums.EAccountType;
import com.simon.mengkou.data.enums.ESmsType;
import com.simon.mengkou.future.base.OuerException;
import com.simon.mengkou.future.base.OuerFutureListener;
import com.simon.mengkou.system.constant.CstOuer;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.ui.base.BaseSocialLoginSystemActivity;
import com.simon.mengkou.utils.UtilOuer;
import com.simon.mengkou.utils.UtilValidate;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/3/1.
 * desc    : 登录界面
 */
public class LoginActivity extends BaseSocialLoginSystemActivity{
    //手机号码
    @Bind(R.id.login_id_phone)
    EditText mEtPhone;
    //密码
    @Bind(R.id.login_id_passwd)
    EditText mEtPassword;

    @Override
    protected void initSystem() {

    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_login);
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        registerAction(CstOuer.BROADCAST_ACTION.LOGINED_ACTION);
    }

    @Override
    public void onReceive(Intent intent) {
        super.onReceive(intent);
        String action = intent.getAction();
        //登录后关闭界面
        if(CstOuer.BROADCAST_ACTION.LOGINED_ACTION.equals(action)) {
            finish();
        }
    }


    /**
     * 关闭登录界面
     */
    @OnClick(R.id.login_id_close)
    protected void close() {
        finish();
    }

    @OnClick(R.id.login_id_login)
    protected void login() {
        //手机号码
        String account = mEtPhone.getText().toString().trim();
        if(UtilString.isBlank(account)) {
            UtilOuer.toast(this, R.string.login_phone_empty);
            mEtPhone.requestFocus();
            return;
        }

        if(!UtilValidate.isPhone(account)) {
            UtilOuer.toast(this, R.string.login_phone_error);
            mEtPhone.requestFocus();
            return;
        }

        //密码
        String password = mEtPassword.getText().toString().trim();
        if(UtilString.isBlank(password)) {
            UtilOuer.toast(this, R.string.login_password_empty);
            mEtPassword.requestFocus();
            return;
        }

        if(password.length() <6 || password.length()>12) {
            UtilOuer.toast(this, R.string.login_password_error);
            mEtPassword.requestFocus();
            return;
        }

        //登录
        LoginReq req = new LoginReq();
		req.setType(EAccountType.PHONE);
		req.setPhone(account);
		req.setPassWord(UtilMD5.getStringMD5(password));
		login(req);
    }


    /**
     * 微信登录、注册
     */
    @OnClick(R.id.login_id_wx)
    protected void loginByWx() {
        loginWeixin();
    }

    /**
     * qq登录、注册
     */
    @OnClick(R.id.login_id_qq)
    protected void loginByQQ() {
        loginQQ();
    }

    /**
     * 新浪微博登录、注册
     */
    @OnClick(R.id.login_id_sina)
    protected void loginBySina() {
        loginSina();
    }

    @Override
    public void onLoginSina(SinaAccessToken token) {
        LoginReq req = new LoginReq();
        req.setType(EAccountType.SINA);
        req.setAuthToken(token.getAccessToken());
        req.setRefreshToken(token.getRefreshToken());
        req.setExpiredTime(token.getExpiresTime());
        req.setUid(token.getUid());
        login(req);
    }

    @Override
    public void onLoginQQ(QQAccessToken token) {
        LoginReq req = new LoginReq();
        req.setType(EAccountType.QQ);
        req.setAuthToken(token.getAccessToken());
        req.setExpiredTime(token.getExpiresTime());
        req.setOpenId(token.getOpenId());
        login(req);
    }

    @Override
    public void onLoginWeixin(WeixinAccessToken token) {
        LoginReq req = new LoginReq();
        req.setType(EAccountType.WECHAT);
        req.setAuthToken(token.getAccessToken());
        req.setRefreshToken(token.getRefreshToken());
        req.setExpiredTime(token.getExpiresTime());
        req.setOpenId(token.getOpenId());
        login(req);
    }

    /**
     * 手机注册
     */
    @OnClick(R.id.login_id_register)
    protected void presentRegister() {
        OuerDispatcher.presentUserPhoneActivity(this, ESmsType.SMS_TYPE_REGISTER);
    }

    /**
     * 忘记密码
     */
    @OnClick(R.id.login_id_forgot)
    protected void presentForgot() {
        OuerDispatcher.presentUserPhoneActivity(this, ESmsType.SMS_TYPE_FORGET_PWD);
    }


    /**
	 * 登录
	 * @param req
	 */
	private void login(LoginReq req) {
		AgnettyFuture future = OuerApplication.mOuerFuture.login(req,
				new OuerFutureListener(this){

					@Override
					public void onStart(AgnettyResult result) {
						super.onStart(result);
						setWaitingDialog(true);
					}

					@Override
					public void onComplete(AgnettyResult result) {
						 super.onComplete(result);
						setWaitingDialog(false);
                        OuerDispatcher.presentMainTabActivity(LoginActivity.this);
                        finish();
					}

					@Override
					public void onException(AgnettyResult result) {
						super.onException(result);
						setWaitingDialog(false);

						Exception ex = result.getException();
						if(ex instanceof OuerException) {
							UtilOuer.toast(LoginActivity.this, ex.getMessage());
						}else {
                            UtilOuer.toast(LoginActivity.this, R.string.common_login_failure);
						}
					}

					@Override
					public void onNetUnavaiable(AgnettyResult result) {
						super.onNetUnavaiable(result);
                        setWaitingDialog(false);
					}

			});

		//添加到销毁队列
		attachDestroyFutures(future);
	}
}
